package com.wakala.employment.fragment.message;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.wakala.employment.R;
import com.wakala.employment.activity.ContactChatActivity;
import com.wakala.employment.adapter.bean.OptionBean;
import com.wakala.employment.adapter.bean.TabEntity;
import com.wakala.employment.core.BaseFragment;
import com.wakala.employment.databinding.FragmentMessageListBinding;
import com.wakala.employment.http.bean.MessageInfo;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.tip.ToastUtils;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MessageListFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
@Page(anim = CoreAnim.none)
public class MessageListFragment extends BaseFragment<FragmentMessageListBinding> implements View.OnClickListener {

    private CommonTabLayout tlMessageNav = null;

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    private final String[] mTitles = {
            "全部", "新招呼", "我发起的"
    };

    private int[] mIconUnselectIds = {
            R.drawable.icon_star, R.drawable.icon_star,
            R.drawable.icon_star, R.drawable.icon_star};
    private int[] mIconSelectIds = {
            R.drawable.icon_star_fill_king, R.drawable.icon_star_fill_king,
            R.drawable.icon_star_fill_king, R.drawable.icon_star_fill_king};
    private List<OptionBean> list;

    //
    private ListView listView;
    private CommonAdapter<MessageInfo> listViewAdapter;
    private List<MessageInfo> messageInfoList = new ArrayList<>();


    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public MessageListFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MessageListFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MessageListFragment newInstance(String param1, String param2) {
        MessageListFragment fragment = new MessageListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected void initViews() {
        tlMessageNav = findViewById(R.id.tl_message_nav);
        list = new ArrayList<>();
        list.add( new OptionBean("0","全部"));
        list.add( new OptionBean("1","新招呼"));
        list.add( new OptionBean("2","我发起的"));

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        tlMessageNav.setTabData(mTabEntities);
        tlMessageNav.setCurrentTab(0);
        getMessageList(tlMessageNav.getCurrentTab());
        tlMessageNav.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                getMessageList(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        listView = findViewById(R.id.listview);
        listViewAdapter = new CommonAdapter<MessageInfo>(getContext(),R.layout.item_message_list,messageInfoList) {

            @Override
            protected void convert(ViewHolder viewHolder, MessageInfo messageInfo, int i) {
                ImageView imageView = viewHolder.getView(R.id.tv_avatar);
                if(!StringUtils.isEmpty(messageInfo.getAvatar())){
                    Glide.with(requireContext()).asBitmap()
                            .load(messageInfo.getAvatar()).placeholder(R.drawable.icon_img_loading)
                            .override(DensityUtils.dp2px(requireContext(),36), DensityUtils.dp2px(requireContext(),36))
                            .transform(new RoundedCorners(10))
                            .apply(new RequestOptions()
                                    .centerCrop()
                                    .placeholder(R.color.color_f4)
                                    .diskCacheStrategy(DiskCacheStrategy.ALL))
                            .into(imageView);
                }
                viewHolder.setText(R.id.tv_name,messageInfo.getName());
                if(!StringUtils.isEmpty(messageInfo.getTitle())){
                    viewHolder.setText(R.id.tv_title,messageInfo.getTitle());
                }
                viewHolder.setText(R.id.tv_summary,messageInfo.getSummary());
                viewHolder.setText(R.id.tv_msg_time,messageInfo.getMsgTime());
                if(null!=messageInfo.getMsgNum() && messageInfo.getMsgNum()>=1){
                    viewHolder.setText(R.id.tv_msg_num,String.valueOf(messageInfo.getMsgNum()));
                }
                viewHolder.setOnClickListener(R.id.fl_item, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.e("TAG", "onClick: "+messageInfo.getName());
                        if(messageInfo.getType().equals("sys")){
                            openNewPage(SystemNoticeFragment.class);
                        }
                        //requireActivity().startActivity(new Intent(getContext(), ContactChatActivity.class));
                        //getActivity().getSupportFragmentManager().beginTransaction().add(ChatFragment.newInstance(messageInfo.getName()),"chat").commit();
                    }
                });



            }
        };
        //listView绑定适配器
        listView.setAdapter(listViewAdapter);
    }

    private void getMessageList(int tabIndex){
        messageInfoList.clear();
        if(0==tabIndex){
            //所有
            MessageInfo messageInfo = new MessageInfo();
            messageInfo.setType("sys");
            messageInfo.setId(1);
            messageInfo.setName("系统消息");
            messageInfo.setTitle("");
            messageInfo.setSummary("提醒您尽快进行实名认证提醒您尽快进行实名认证提醒您尽快进行实名认证提醒您尽快进行实名认证提醒您尽快进行实名认证提醒您尽快进行实名认证提醒您尽快进行实名认证提醒您尽快进行实名认证");
            messageInfo.setMsgNum(1);
            messageInfo.setMsgTime("2024-10-11");
            messageInfoList.add(messageInfo);
        }else if (1==tabIndex){
            //新招呼
        }else if (2==tabIndex){
            //我发起的
        }
        if(messageInfoList.isEmpty()){
            binding.includeEmptyView.llContent.setVisibility(View.VISIBLE);
        }else {
            binding.includeEmptyView.llContent.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View view) {

    }

    @NonNull
    @Override
    protected FragmentMessageListBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentMessageListBinding.inflate(inflater, container, attachToRoot);
    }
    @Override
    protected void initArgs() {
        super.initArgs();
    }
    /**
     * @return 返回为 null意为不需要导航栏
     */
    @Override
    protected TitleBar initTitle() {
        return null;
    }
}